package middlewares

import (
    "github.com/kataras/iris/v12/context"
    "net/http"
)

// BusErrHandler 业务异常
type BusErrHandler struct {
    ErrCode int
    Message string
}

// ErrorHandler 业务异常统一处理
func ErrorHandler(ctx context.Context) {
    defer func() {
        if r := recover(); r != nil {
            switch t := r.(type) {
            // 业务异常
            case BusErrHandler:
                ctx.StatusCode(http.StatusBadRequest)
                ctx.JSON(t)
            default:
                ctx.StatusCode(http.StatusInternalServerError)
                ctx.JSON(t)
            }
        }
    }()

    ctx.Next()
}
